package com.ys.controller.api.overseas;

import com.ys.config.MenuId;
import com.ys.entity.overseas.TosPurchasePlanItem;
import com.ys.service.overseas.TosPurchasePlanItemService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-19
 * @Description: HW采购发货
 * @Version: 1.5
 */
@CrossOrigin
@Controller
@RequestMapping("/api/inter/tosPurchasePlanItemFh")
public class TosPurchasePlanItemFhController {
    private static final Logger logger = LoggerFactory.getLogger(TosPurchasePlanItemFhController.class);

    @Autowired
    private TosPurchasePlanItemService tosPurchasePlanItemService;

    @Autowired
    private CommonApiService<TosPurchasePlanItem> commonApiService;

    Long MENUID = MenuId.TOS_PurchasePlanItemFh;


    public TosPurchasePlanItem setSeachSql(TosPurchasePlanItem cond){
        cond.addLeftJoin("TOS_PurchasePlanSon B ON A.PurchasePlanSonId = B.id")
        .addField("B.productImg,B.code,B.productName,B.purchaseAmount,B.deliveryAmount,B.purchaseNum,B.stockNum,B.orderCode,B.consignee,B.addressLine,B.city,B.postalCode,B.phone")
        .addLeftJoin("TOS_PurchasePlan C ON C.bid = B.pbid")
        .addField("C.bid hwPlanBid,C.vgsBm,C.vgsJc,C.shopName,C.purchaseTotal,C.purchaseTotalPrice,C.remark planRemark")
        .addLeftJoin("TOS_PlatformInformation D ON B.platformId = D.id")
        .addField("D.platformCode,D.hwName,D.regionName,D.currencyCode")//平台信息
        .addField("A.status _ys_status")
        ;
        return cond;
    }


    @RequiresPermissions("tosPurchasePlanItemFh:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TosPurchasePlanItem cond, String conditions
    ){

        setSeachSql(cond);
        String orderStatus = ConditionUtil.getConditionValue(conditions, "_ys_status");//订单状态
        if (orderStatus == null || "".equals(orderStatus)){
            cond.where("A.status = 4");
        }else if(!orderStatus.equals("7")){
            cond.where("A.status  = "+orderStatus);
        }else {
            cond.where("A.status > 3");
        }


        cond = commonApiService.dataAuth(MENUID, cond);
        return commonApiService.searchLay(cond);
    }

    /**
     * 发货取消
     */
    @RequiresPermissions("tosPurchasePlanItemFh:qx")
    @PostMapping("/qx")
    @ResponseBody
    public R qx(HttpServletRequest request, Long id ){
        try {
            return tosPurchasePlanItemService.fhqx(id);
        } catch (Exception e) {
            logger.error("HW采购发货取消出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 追踪编码导入
     */
    @RequiresPermissions("tosPurchasePlanItemFh:dr")
    @PostMapping("/dr")
    @ResponseBody
    public R dr(HttpServletRequest request, Long id ,String tracingCode,String commodityProperty,String orderCode){
        try {
            return tosPurchasePlanItemService.dr(id,tracingCode,commodityProperty,orderCode);
        } catch (Exception e) {
            logger.error("HW采购结算审核出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }
}
